package com.enterprise.projectManager.service.implemention;

import com.enterprise.projectManager.model.Log;
import com.enterprise.projectManager.model.Project;
import com.enterprise.projectManager.rope.LogRope;
import com.enterprise.projectManager.rope.ProjectRope;
import com.enterprise.projectManager.service.LogService;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
public class LogImpl implements LogService {
    private final LogRope logRope;
    private final ProjectRope projectRope;
    private final CountServiceImpl countService;
    @Override
    public Log create(Log log) {
        if (log.getLogId() == null)
            log.setLogId(countService.generalLogId());
        return logRope.save(log);
    }

    @Override
    public Boolean delete(String logId) {
        Optional<Log> optionalLog = logRope.findById(logId);
        logRope.delete(optionalLog.get());
        return Boolean.TRUE;    //待进一步判断
    }

    @Override
    public Log update(Log log) {
        return logRope.save(log);
    }

    //查询某一个项目的所有日志
    @Override
    public List<Log> searchByProjectId(String projectId) {
        List<Log> logs = logRope.findByProjectProjectId(projectId);
        return logs;
    }

    @Override
    public Boolean releaseLog() {
        LocalDate currentDate = LocalDate.now();

        int year = currentDate.getYear();
        int month = currentDate.getMonthValue();
        int day = currentDate.getDayOfMonth();

        String formattedMonth = String.format("%02d", month); // 月份始终占两个字符
        String formattedDay = String.format("%02d", day); // 日期始终占两个字符

        List<Project> projects = projectRope.findAll();
        for (int i = 0; i < projects.size(); i++) {
            String formattedIndex = String.format("%03d", i+1); // 数字 i 始终占三个字符
            String newLogId=String.valueOf(year) + formattedMonth + formattedDay + formattedIndex;
            if(!isExist(newLogId))
            {
                System.out.println(newLogId);
                Log newLog =new Log(projects.get(i),newLogId,null,"0",new Date(),new Date(),"wuwuwuwuwu","zhangsan");
                logRope.save(newLog);
            }
        }

        return true;
    }

    @Override
    public Boolean isExist(String id) {
        return logRope.existsById(id);
    }

    @Override
    public JsonNode formatLog(Log log) {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.valueToTree(log);
        Date now = new Date();
        if (now.after(log.getLogBeyondDate()))
        {
            ((ObjectNode) jsonNode).put("isDelay","是");
        }else ((ObjectNode) jsonNode).put("isDelay","否");
        return jsonNode;
    }

    @Override
    public List<JsonNode> getAll() {
        List<Log>  logs = logRope.findAll();
        List<JsonNode> jsonNodeList = new ArrayList<>();
        logs.forEach(log -> {
            jsonNodeList.add(formatLog(log));
        });
        return jsonNodeList;
    }

    @Override
    public Boolean isLogBeyondDateOneDay(Log log,LocalDate localDate) {
        Date date = log.getLogBeyondDate();
        // 将java.util.Date对象转换为java.time.LocalDate对象
        localDate = date.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 判断日期是否是今天
        if (localDate.equals(today)) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }
}
